package db

import (
	"github.com/spf13/viper"
	"gorm.io/gorm"
)

type Dber interface {
	connect() *gorm.DB
}

var Db *gorm.DB

func connect(d Dber) *gorm.DB {
	return d.connect()
}

func New() {
	defaultDb := viper.GetString("default_db")

	switch defaultDb {
	case "postgre":
		dbConfig := PostgreSQLConfig{
			Host:     viper.GetString("postgre.host"),
			Port:     viper.GetString("postgre.port"),
			Database: viper.GetString("postgre.database"),
			Username: viper.GetString("postgre.username"),
			Password: viper.GetString("postgre.password"),
			Charset:  viper.GetString("postgre.charset"),
		}
		Db = connect(&dbConfig)
	default:
		dbConfig := MysqlConfig{
			Host:     viper.GetString("mysql.host"),
			Port:     viper.GetString("mysql.port"),
			Database: viper.GetString("mysql.database"),
			Username: viper.GetString("mysql.username"),
			Password: viper.GetString("mysql.password"),
			Charset:  viper.GetString("mysql.charset"),
		}

		Db = connect(&dbConfig)
	}
}
